package com.filldream.fastboot.web.controller.pc;

import com.filldream.fastboot.base.entity.SystemUser;
import com.filldream.fastboot.base.request.IdSTO;
import com.filldream.fastboot.base.request.pc.systemUser.*;
import com.filldream.fastboot.base.response.pc.SystemLoginDTO;
import com.filldream.fastboot.common.annotation.Log;
import com.filldream.fastboot.common.annotation.RolePermissions;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.ErrorCode;
import com.filldream.fastboot.common.entity.P;
import com.filldream.fastboot.common.entity.R;
import com.filldream.fastboot.common.util.Assert;
import com.filldream.fastboot.common.util.TransferUtil;
import com.filldream.fastboot.core.LoginHolder;
import com.filldream.fastboot.core.service.SystemUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@Api(tags = "PC-管理员管理")
@RestController
@RequestMapping("/pc/systemUser")
public class PcSystemUserController {

    @Autowired
    private SystemUserService systemUserService;

    @Log(value = "用户登录PC端管理")
    @RolePermissions("")
    @ApiOperation(value = "用户登录", notes = "后台系统用户登录")
    @PostMapping(value = "/login")
    public R<SystemLoginDTO> login(@RequestBody @Valid SystemLoginSTO systemLoginSTO, HttpSession httpSession)  {
        return systemUserService.doLogin(systemLoginSTO,httpSession.getId());
    }

    @Log(value = "用户退出PC端管理")
    @ApiOperation(value = "用户退出", notes = "后台系统用户退出")
    @PostMapping(value = "/loginOut")
    public B loginOut(HttpServletRequest request, HttpSession session, HttpServletResponse response) {
        return systemUserService.loginOut(request,session,response);
    }

    @Log(value = "修改当前管理员密码")
    @ApiOperation(value = "修改当前管理员密码", notes = "修改当前管理员密码")
    @PostMapping(value = "/modifyPassword")
    @RolePermissions("sys_user:modifyPassword")
    public B modifyPassword(@RequestBody @Valid ModifyPasswordSTO modifyPasswordSTO) {
        return systemUserService.modifyPassword(modifyPasswordSTO);
    }

    @Log(value = "管理员列表")
    @ApiOperation(value = "管理员列表")
    @GetMapping(value = "/list")
    @RolePermissions("sys_user:list")
    public P systemUserList(@Valid SystemUserListSTO systemUserListSTO) {
        return systemUserService.systemUserList(systemUserListSTO);
    }

    @Log("添加管理员")
    @ApiOperation(value = "添加管理员")
    @PostMapping(value = "/add")
    @RolePermissions("sys_user:add")
    public R<SystemUser> add(@RequestBody @Valid AddSystemUserSTO addSystemUserSTO) {
        SystemUser systemUser = TransferUtil.changeToObj(addSystemUserSTO, SystemUser.class);
        systemUser.setRoleId(addSystemUserSTO.getRoleId().get(0));
        return systemUserService.addSystemUser( systemUser );
    }

    @Log("修改管理员")
    @ApiOperation(value = "修改管理员",notes = "修改管理员")
    @PostMapping(value = "/modify")
    @RolePermissions("sys_user:modify")
    public B modify(@RequestBody @Valid MofiySystemUserSTO mofiySystemUserSTO) {
        SystemUser systemUser = TransferUtil.changeToObj(mofiySystemUserSTO, SystemUser.class);
        systemUser.setRoleId(mofiySystemUserSTO.getRoleId().get(0));
        return systemUserService.modifySystemUser(systemUser);
    }

    @Log("禁用或启用管理员")
    @ApiOperation(value = "禁用启用管理员",notes = "禁用启用管理员")
    @PostMapping(value = "/modifyStatus")
    @RolePermissions("sys_user:modifyStatus")
    public B modifyStatus(@RequestBody @Valid MofiySystemUserStatusSTO mofiySystemUserStatusSTO) {
        return systemUserService.modifySystemUserStatus(mofiySystemUserStatusSTO.getId(),mofiySystemUserStatusSTO.getStatus());
    }

    @Log("删除管理员")
    @ApiOperation(value = "删除管理员")
    @PostMapping(value = "/del")
    @RolePermissions("sys_user:del")
    public B del(@RequestBody IdSTO idSTO) {
        Assert.checkBoolean( !idSTO.getId().equals(1L), ErrorCode.BASE_ERR.reMsg("超级管理员不允许删除!"));
        SystemUser systemUser = new SystemUser();
        systemUser.setId(idSTO.getId());
        systemUser.setIsDel(1);
        return systemUserService.modifySystemUser(systemUser);
    }

    @Log("查询当前用户是否需要验证码")
    @ApiOperation(value = "当前用户是否需要验证码")
    @GetMapping(value = "/validCaptcha")
    @RolePermissions("sys_user:validCaptcha")
    public R<Boolean> validCaptcha(String account) {
        return systemUserService.validCaptcha(account);
    }

    @Log("查询当前登陆人信息")
    @ApiOperation(value = "查询当前登陆人信息")
    @GetMapping(value = "/info")
    @RolePermissions
    public R<SystemLoginDTO> info() {
        return R.successd( LoginHolder.getPcUser());
    }

    @Log(value = "重置用户密码")
    @ApiOperation(value = "重置用户密码", notes = "重置用户密码")
    @PostMapping(value = "/resetPwd")
    @RolePermissions("sys_user:resetPwd")
    public B resetPwd(@RequestBody ModifyPasswordSTO modifyPasswordSTO) {
        return systemUserService.resetPwd(modifyPasswordSTO);
    }

    @RolePermissions
    @Log(value = "修改当前用户头像")
    @ApiOperation(value = "修改当前用户头像", notes = "修改当前用户头像")
    @PostMapping(value = "/modifyImg")
    public B modifyImg(@RequestBody MofiySystemUserSTO mofiySystemUserSTO) {
        SystemUser systemUser = new SystemUser();
        systemUser.setId( LoginHolder.getPcUserId() );
        systemUser.setImg( mofiySystemUserSTO.getImg() );
        return B.checkSuccess(systemUserService.updateById( systemUser ));
    }

}
